package com.kyrioscraft.cloud.thead.jdklab.tools.reentrant;

import java.util.concurrent.locks.ReentrantLock;

//可以多次加锁和释放锁
public class ReentrantLockDemo {
    public static ReentrantLock lock = new ReentrantLock(true);
    public static int i=0;
    public static void main(String[] args) throws Exception{
        Runnable r = ()->{
            for(int j=0; j<100000; j++){
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"获得锁");
                try {
                    i++;
                }finally {
                    lock.unlock();
                }
            }
        };
        Thread t1 = new Thread(r,"thread1");

        Thread t2 = new Thread(r,"thread2");

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(i);
    }
}
